#include <cstdlib>
#include <cstdio>
#include <iostream>
#include <string.h>
#include "./include/operate_config.h"
#include "./include/ftp.h"
#include "./include/kafka.h"

namespace operateconfig
{
	class CConfig;
}
namespace operateconfig
{
	class File_not_found;
}

namespace operateconfig
{
	class Key_not_found;
}

int main(int argc, char **argv)
{
	Cmd cmd_ctx;
	Cmd *cmd = &cmd_ctx;

	cmd_ctx.config_file = "./cfg/config.txt"; //默认配置文件
	cmdconfig::parse_options(argc, argv, cmd);
	std::cout << "Config_File_Name:" << cmd_ctx.config_file << std::endl;

	// 读配置文件，失败返回一个 异常类对象
	try
	{
		// Code that could throw an exception
		ConfigHandle.init(cmd_ctx.config_file);
	}
	catch (operatorconfig::File_not_found &filenf)
	{
		std::cerr << "**Warring: Attemped to open no exist file <" << filenf.filename << ">." << std::endl;
		return EXIT_FAILURE;
	} 
	// 获取ftp配置参数
	cmd_ctx.ftp.ftp_Ip = ConfigHandle.read<std::string>("ftp_Ip");				// 读取ftp服务器的ip地址
	ConfigHandle.readInto<std::string>(cmd_ctx.ftp.ftp_Port, "ftp_Port", "21"); // 读取端口号,默认21端口
	cmd_ctx.ftp.ftp_UserName = ConfigHandle.read<std::string>("ftp_UserName");	// ftp用户名
	cmd_ctx.ftp.ftp_Password = ConfigHandle.read<std::string>("ftp_Password");	// ftp密码
	cmd_ctx.ftp.ftp_SavePath = ConfigHandle.read<std::string>("ftp_SavePath");

	CFTPManager myftp;
	myftp.parseShow(cmd_ctx.ftp);
	myftp.login2Server(cmd_ctx.ftp.ftp_Ip, cmd_ctx.ftp.ftp_Port);
	myftp.inputUserName(cmd_ctx.ftp.ftp_UserName);
	myftp.inputPassWord(cmd_ctx.ftp.ftp_Password);
	std::string dir = myftp.Dir("/home/tgm/configcpp/cmd_config");

	//获取kafka配置参数
	cmd_ctx.kafka.kafka_Ip = ConfigHandle.read<std::string>("kafka_Ip");

	KAFKAManager *mykafka = new (KAFKAManager);
	mykafka->createProducer(cmd_ctx.kafka.kafka_Ip);

	std::string myVual = "testVual";
	std::string myKey = "testKey";
	std::string myTopic = "testTopic";

	mykafka->produceMessage(myTopic.c_str(), const_cast<char *>(myVual.c_str()), myVual.size(), const_cast<char *>(myKey.c_str()), myKey.size());
	delete mykafka;

	//myftp.quitServer();
	return EXIT_SUCCESS;
}
